<?php

/**
 * Return an array of random friends of the current user
 * @param type <object> $fb
 * @return type <array> $my_five_friends
 */
function friendsplus_getrandomfriends($fb){
    $friends = $fb->api('/me/friends');
    $myfriends = $friends['data'];
    
    
    foreach($myfriends as $my_friend){
        $friends_results[$my_friend['id']][] = $my_friend['name'];
    }
    
    if(count($myfriends) > 15){
        $my_five_friends = array_rand($friends_results,15);
    }else{
        $my_five_friends = array_rand($friends_results,count($my_five_friends));
    }
    
    return $my_five_friends;
  
}

/**
 * Return an array contening informations of five friends
 * @param type $fb
 * @param type $type_question 
 */

function friendsplus_getfivefriends($fb, $type_question){

    
        switch($type_question){
            case '1':
                $filtre = 'hometown';
            break;
            case '2':
                $filtre = 'birthday ';
            break;
        }

        $my_five_friends = friendsplus_getrandomfriends($fb);

        foreach($my_five_friends as $friend){
            print $filtre;
            $profil = $fb->api('/'.$friend);
            if(isset($profil[$filtre])){
                $resultat[] = array('id' => $profil['id'], 'first_name' => $profil['first_name'], 'last_name' => $profil['last_name'], $filtre => $profil[$filtre]);
            }
            
            if(@count($resultat) == 5){ // sans le @ affiche une erreur...
                break;
            }

        }
        
        if(@count($resultat) == 0 ){
            $my_five_friends = friendsplus_getrandomfriends($fb);
        }
        
        return $resultat;
}


/**
 * Return a random question
 * @return type 
 */
function friendsplus_getquestion(){
    $connexion = new PDO_Mysql();
    $resultat = $connexion->query('SELECT t.id_type_question as type_question, question, type as nom_type 
                                FROM question q, type_question t
                                WHERE q.id_type_question = t.id_type_question
                                ORDER BY RAND()
                                LIMIT 1');
    
    $resultat->setFetchMode(PDO_Mysql::FETCH_OBJ);
    $question = $resultat->fetch();
    return array($question->type_question, $question->question, $question->nom_type);
}

?>
